package com.TinyBBS.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;
import java.util.Random;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.TinyBBS.db.Config;

public class Util {
	/**
	 * Get current date and time as TimeStamp data type
	 * 
	 * @return Date
	 * @throws ParseException
	 */
	public static Date getCurrentTimeStamp(){
		Date date = new Date();
		try{
			date = new GregorianCalendar().getTime();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * Convert password to String encrypted in md5 algorithm
	 * 
	 * @param password
	 * @return String
	 */
	public static String getHash(String password) {
		MessageDigest alg = null;
		try {
			alg = MessageDigest.getInstance("MD5");
			alg.update(password.getBytes());
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (new BigInteger(1, alg.digest()).toString(16));
	}

	/**
	 * Convert password to String encrypted in md5 algorithm with salt
	 * 
	 * @param password
	 * @param salt
	 * @return String
	 */
	public static String getHashWithSalt(String password, String salt) {
		String newPass = getHash(password) + salt;
		return getHash(newPass);
	}
	/**
	 * Generate random number by passing range
	 * @param aStart
	 * @param aEnd
	 * @param random
	 * @return Integer
	 */
	public static String GenerateInteger(int aStart, int aEnd, Random random) {
		if (aStart > aEnd) {
			throw new IllegalArgumentException("Start cannot exceed End.");
		}
		// get the range, casting to long to avoid overflow problems
		long range = (long) aEnd - (long) aStart + 1;
		// compute a fraction of the range, 0 <= frac < range
		long fraction = (long) (range * random.nextDouble());
		int randomNumber = (int) (fraction + aStart);
		
		return String.valueOf(randomNumber);
	}

	/**
	 * Send SSL message (eg.gmail)
	 * 
	 * @param recipients
	 * @param subject
	 * @param message
	 * @param from
	 * @throws MessagingException
	 */
	public static void sendSSLMessage(String recipients[], String subject,
			String message, String from) throws MessagingException {
		boolean debug = true;

		Properties props = new Properties();
		props.put("mail.smtp.host", Config.SMTP_HOST_NAME);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", Config.SMTP_PORT);
		props.put("mail.smtp.socketFactory.port", Config.SMTP_PORT);
		props.put("mail.smtp.socketFactory.class", Config.SSL_FACTORY);
		props.put("mail.smtp.socketFactory.fallback", "false");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(Config.EMAIL_FROM,
								Config.EMAIL_FROM_PASS);
					}
				});

		session.setDebug(debug);

		Message msg = new MimeMessage(session);
		InternetAddress addressFrom = new InternetAddress(from);
		msg.setFrom(addressFrom);

		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		// Setting the Subject and Content Type
		msg.setSubject(subject);
		msg.setContent(message, "text/html");
		Transport.send(msg);
	}

}
